const db = require('../../db')
/**
 * POST 通知
 */
exports.add = (req, res) => {
  // 接收表单的数据
  const noticeInfo = req.body
  let sql1 = 'insert into account_notice set ?'
  db.query(sql1, {
    remark: noticeInfo.remark,
    title: noticeInfo.title,
    createdTime: Date.now(), // 例如 createdTime 使用 Date.now() 来获取当前时间戳
  }, (error, result) => {
    if (error) return res.cc(error)
    // 判断影响行数是否为 1
    if (result.affectedRows !== 1) return res.cc('新增记录失败！')
    return res.cc('新增记录成功', true)
  })
}

exports.del = (req, res) => {
  // 接收表单的数据
  const noticeInfo = req.body
  let sql = 'select * from account_notice where id=?'
  db.query(sql, [noticeInfo.id], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该记录!')
    }
    // 定义 SQL 语句
    const sql1 = 'delete from account_notice where id=?'
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, noticeInfo.id, (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('删除记录失败！')
      return res.cc('删除记录成功', true)
    })
  })
}

exports.update = (req, res) => {
  // 接收表单的数据
  const noticeInfo = req.body
  let sql = 'select * from account_notice where id=?'
  db.query(sql, [noticeInfo.id], (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该记录!')
    }
    // 定义 SQL 语句
    const sql1 = "update account_notice set remark=?, type=?, title=? where id=?";
    // 调用 db.query() 执行 sql 语句
    db.query(sql1, [noticeInfo.remark, noticeInfo.type, noticeInfo.title, noticeInfo.id], (error, result) => {
      if (error) return res.cc(error)
      // 判断影响行数是否为 1
      if (result.affectedRows !== 1) return res.cc('修改记录失败！')
      return res.cc('修改记录成功', true)
    })
  })
}

exports.listOne = (req, res) => {
  // 接收表单的数据
  const noticeInfo = req.body
  let sql = 'select * from account_notice where type=1'
  db.query(sql, (error, result) => {
    if (error) {
      return res.cc(error)
    }
    if (result.length === 0) {
      return res.cc('未找到该记录!')
    }
    // 定义 SQL 语句
    return res.cc('修改记录成功', true, result[0])
  })
}

exports.allList = (req, res) => {
  const page = req.body.page ? parseInt(req.body.page) : 1;
  const limit = req.body.limit ? parseInt(req.body.limit) : 10;
  const title = req.body.title || '';
  const sqlCount = "SELECT COUNT(*) AS total FROM account_notice";

  db.query(sqlCount, (error, countResults) => {
    if (error) {
      return res.cc(error);
    }

    const total = countResults[0].total;
    const offset = (page - 1) * limit;
    let sql;
    let params;

    if (title) {
      sql = `SELECT * FROM account_notice WHERE title LIKE ? LIMIT ? OFFSET ?`;
      params = [`%${ title }%`, limit, offset];
    } else {
      // If title is empty, provide default values for params
      sql = `SELECT * FROM account_notice LIMIT ? OFFSET ?`;
      params = [limit, offset];
    }

    db.query(sql, params, (error, results) => {
      if (error) {
        return res.cc(error);
      }

      return res.cc('获取记录成功', true, results, {
        total,
        page,
        limit
      });
    });
  });
};

